// beamgolem.txt
// By Lazarus
// Thanks a bunch to Niemand for all the help with the targeting algorithm.
// Shoots beams that do 22d5 damage of random damage type, and one random status effect.
// If flag is set and char species is golem, then char is killed.
// Random target
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1 - How much to increase flag by.
//   Cell 2 - If add half an exp, then set this to 5.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,status,type,next,new,targ,best;

body;

beginstate INIT_STATE;
	//if(get_flag(10,9) > 1){
	//	if(get_species(ME) == 11)
	//		kill_char(ME,2,0);
	//}
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
break;

beginstate DEAD_STATE;
	if(char_ok(4) == 1){
		if(get_memory_cell(2) == 5)
			inc_flag(250,5,(get_ran(1,get_memory_cell(1),get_memory_cell(1) + 1)));
		if(get_memory_cell(2) != 5)
			inc_flag(250,5,get_memory_cell(1));
	}
break;

beginstate START_STATE; 

	// Look for a target, attack it if visible
	next = 3;
	
	set_state_continue(4);
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		set_state_continue(3);
	}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((target_ok() == FALSE) || (can_see_char(get_target()) == 0))
		set_state(START_STATE);
	if(new == 1){ //Mix things up and choose a new target
		next = 3;
		set_state_continue(4);
	}
	type = get_ran(1,1,6);
	while(type == 4){
		type = get_ran(1,1,6);
	}
	status = get_ran(1,101,110);
	if(status == 101)
		status = 0;
	if(status == 102)
		status = 6;
	if(status == 103)
		status = 7;
	if(status == 104)
		status = 8;
	if(status == 105)
		status = 10;
	if(status == 106)
		status = 11;
	if(status == 107)
		status = 12;
	if(status == 108)
		status = 18;
	if(status == 109)
		status = 20;
	if(status == 110)
		status = 21;
	if(can_see_char(get_target())){
		put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(get_target()),char_loc_y(get_target()),1);
		put_boom_on_char(get_target(),6,0);
		run_animation();
		damage_char(get_target(),get_ran(22,1,5),type);
		print_named_str(ME,"fires a bolt of energy.");
		set_char_status(get_target(),status,6,0,1);
		new = 1;
		deduct_ap(3); 
	}
break;

beginstate 4; //Targetting state
	targ = -1;
	best = 30000;
	i = 0;
	while(i < 120){
		if((char_ok(i) == 1) && (char_attitude_to_char(ME,i) == 2) && (dist_to_char(i) < 12) && can_see_char(i) && (get_char_status(i,29) == 0)){
			status = get_ran(1,0,100);
			if(i < 6) //a party member
				status = get_ran(1,0,50);
			if(status < best){
				targ = i;
				best = status;
			}
		}
		i = i + 1;
	}
	set_target(ME,targ);
	if(targ == -1)
		set_state(START_STATE);
	new = 0;
	set_state_continue(next);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;
